﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BooksOnline.Models;
using BooksOnline.Serivces;

namespace BooksOnline.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public PartialViewResult Index()
        {
            BooksOnline.User user = new User();
            user.Id = 1;

            if (user.Id == 1)
                return PartialView("AdminMenu");
            else
                return PartialView("UserMenu");
        }

        public ActionResult ListAllUsers()
        {
            var userModel = ModelFactory.GetBooksModel();
            ViewData.Model = userModel.Users.ToList();

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult ShowRoles(FormCollection collection)
        {
            var userID = Convert.ToInt32(collection["UserId"]);
            var model = ModelFactory.GetBooksModel();

            var roles = (from userrole in model.UserRoles
                        where userrole.User.Id == userID
                        select userrole.Role).FirstOrDefault();

            //RoleModel roleModel = new RoleModel();
            RoleModel rolesModel = new RoleModel();

            rolesModel.RoleName = roles.Name;
            rolesModel.IsAdmin = roles.IsAdmin;

            //foreach (var role in roles)
            //{
            //    rolesModel.Add(new RoleModel() { IsAdmin = role.IsAdmin, RoleName = role.Name });
            //}
            JsonResult result = Json(rolesModel);
            //roleModel.IsAdmin = roles.IsAdmin;
            //roleModel.RoleName = roles.Name;
            //JsonResult result = Json(roleModel);

            return result;
        }
    }
}
